<template>
  <div>
    <el-card>
      <!-- 充值设置 start -->
      <div class="setting">
        <TitleLine title="充值设置" />
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-form-item label="最高充值" prop="maxIntegral">
            <el-input
              v-model.number="formData.maxIntegral"
              placeholder="请输入最高充值积分"
              :maxlength="7"
              clearable
              :style="{ width: '20%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="最低充值" prop="minIntegral">
            <el-input
              v-model.number="formData.minIntegral"
              placeholder="请输入最低充值积分"
              :maxlength="4"
              clearable
              :style="{ width: '20%' }"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 充值设置 end -->

      <!-- 入驻说明 start-->
      <div class="enter">
        <TitleLine title="入驻说明" />
        <Tinymce v-model="formData.joinContent" />
      </div>
      <!-- 入驻说明 end-->

      <!-- 充值说明 start-->
      <div class="enter">
        <TitleLine title="充值说明" />
        <Tinymce v-model="formData.rechargeContent" />
      </div>
      <!-- 充值说明 end-->

      <!-- 开票帮助 start-->
      <div class="enter">
        <TitleLine title="开票帮助" />
        <Tinymce v-model="formData.invoiceContent" />
      </div>
      <!-- 开票帮助 end-->
    </el-card>

    <div class="flex justify-center mt-20">
      <el-button size="medium" @click="cancle">取消</el-button>
      <el-button size="medium" type="primary" @click="confirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  name: 'CompanySettingParams',
  components: {},
  props: {},
  data() {
    // 充值最高充值积分自定义验证
    const cusMaxIntegral = (rule, value, callback) => {
      if (value > 1000000 || value < 100) {
        callback(new Error('请输入100-1000000之间数值'))
      } else {
        callback()
      }
    }
    // 充值最低充值积分自定义验证
    const cusMinIntegral = (rule, value, callback) => {
      if (value > 1000 || value < 10) {
        callback(new Error('请输入10-1000之间数值'))
      } else {
        callback()
      }
    }

    return {
      // 表单
      formData: {
        // 最高充值积分
        maxIntegral: '',
        // 最低充值积分
        minIntegral: '',
        // 入驻说明
        joinContent: '',
        // 充值说明
        rechargeContent: '',
        // 开票帮助
        invoiceContent: ''
      },
      rules: {
        maxIntegral: [
          {
            required: true,
            message: '请输入最高充值积分',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入整数值', trigger: ['blur', 'change'] },
          { validator: cusMaxIntegral, trigger: ['blur', 'change'] }
        ],
        minIntegral: [
          {
            required: true,
            message: '请输入最低充值积分',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入整数值', trigger: ['blur', 'change'] },
          { validator: cusMinIntegral, trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    // 获取参数设置
    this.getSetting()
  },
  mounted() {},
  methods: {
    // 获取参数设置
    async getSetting() {
      try {
        const { data } = await this.$http.user.parmaSetting.getSetting()
        // console.log(data)
        this.formData = {
          ...data,
          maxIntegral: parseInt(Number(data.maxIntegral) * 100),
          minIntegral: parseInt(Number(data.minIntegral) * 100)
        }
      } catch (error) {
        console.log('getSetting', error)
      }
    },

    // 取消
    cancle() {
      this.$router.back()
    },

    // 确定
    confirm: _.debounce(async function () {
      if (this.formData.minIntegral > this.formData.maxIntegral) {
        this.$message.error('最高充值积分不能小于最低充值积分')
        return
      }
      try {
        this.formData.id = 1
        const { status, msg } = await this.$http.user.parmaSetting.editSetting(this.formData)
        if (status == 200) {
          this.$message.success('编辑成功')
          this.$router.back()
        } else {
          this.$message.error(msg)
        }
      } catch (error) {
        console.log('confirm', error)
      }
    }, 400)
  }
}
</script>

<style lang="scss" scoped></style>
